
package com.wwy.monitor.service.acl.helper;

import com.alibaba.buc.acl.api.input.check.CheckActionsParam;
import com.alibaba.buc.acl.api.input.check.CheckPermissionsParam;
import com.alibaba.buc.acl.api.output.check.CheckActionsResult;
import com.alibaba.buc.acl.api.service.AccessControlService;
import com.alibaba.buc.api.ActionService;
import com.alibaba.buc.api.PermissionService;
import com.alibaba.buc.api.exception.BucException;
import com.alibaba.buc.api.permission.GetPermissionParam;
import com.alibaba.buc.api.permission.PermissionResult;
import com.wwy.monitor.service.acl.AclInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Title: UrlCheckServiceHelper
 * Description:UrlCheckServiceçå¸®å©ç±»
 * Date: 16/5/11 ä¸å1:50
 *
 * @author jl107065 jl107065@autonavi.com
 * @see
 */
@Service
public class UrlCheckServiceHelper {
    @Autowired
    private AclInfo aclInfo;
    @Autowired
    private CheckActionsParam checkActionsParam;
    @Autowired(required = false)
    private ActionService actionService;
    @Autowired(required = false)
    private PermissionService permissionService;
    @Autowired
    private AccessControlService accessControlService;
/**
 * è°ç¨ACLæ¥å£æ§è¡URLæééªè¯
 *
 * @param userId   BUC ç¨æ·id éç¨æ·å·¥å·
 * @param pathname è¢«æ£æ¥çurl
 * @return List<CheckActionsResult>
 */
/*public List<CheckActionsResult> doCheckActions(Long userId, String pathname) throws BucException {
if (actionService == null) {
return null;}
if (userId == null) {
return null;}
// åå»ºå¥å
CheckActionsParam checkActionsParam = new CheckActionsParam();
checkActionsParam.setUserId(userId.intValue());
checkActionsParam.setAppName(aclInfo.getAppname());
List<String> patterns = new ArrayList<String>();
// è®¾ç½®è¦éªè¯ç URL æé
patterns.add(pathname);
checkActionsParam.setPatterns(patterns);
return actionService.checkActions(checkActionsParam);
}*/

    /**
     * è·åæéçè¯¦ç»åç§°
     *
     * @param permissionName
     * @return
     * @throws BucException
     */
    public PermissionResult getPermissionByName(String permissionName) throws BucException {
        if (permissionService == null) {
            return null;
        }
// åå»ºå¥å
        GetPermissionParam getPermissionParam = new GetPermissionParam();
// è®¾ç½®getPermissionParam
        getPermissionParam.setPermissionName(permissionName);
// å¾å°æéæ¥è¯¢ç»æ
        return permissionService.getPermissionByName(getPermissionParam);
    }

    /**
     * æ°çaclæ¥å£
     *
     * @param userId
     * @param pathname
     * @return
     * @throws BucException
     */
    public CheckActionsResult doCheckAccessControle(Integer userId, String pathname) throws BucException {
        if (actionService == null) {
            return null;
        }
        if (userId == null) {
            return null;
        }
// åå»ºå¥å
        checkActionsParam.setUserId(userId.intValue());
        List<String> patterns = new ArrayList<String>();
// è®¾ç½®è¦éªè¯ç URL æé
        patterns.add(pathname);
        checkActionsParam.setPatterns(patterns);
        CheckActionsResult checkActionsResult = accessControlService.checkActions(checkActionsParam);
        checkActionsResult.getCheckActionResults().stream().forEach(e -> e.setAccessible(true));
        return checkActionsResult;
    }
}